package com.zbkj.service.service.water.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.water.RzPackage;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.water.RzPackageRequest;
import com.zbkj.common.request.water.SearchRzPackageRequest;
import com.zbkj.common.response.water.RzPackageResponse;
import com.zbkj.service.dao.water.RzPackageDao;
import com.zbkj.service.service.water.RzPackageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐实现类
 */
@Service
public class RzPackageServiceImpl extends ServiceImpl<RzPackageDao, RzPackage> implements RzPackageService {

    /**
     * 添加修改套餐
     *
     * @param request 添加修改套餐请求对象
     * @return boolean
     */
    @Override
    public boolean saveOrUpdatePackage(RzPackageRequest request) {
        //套餐名称重复校验
        validateName(request);
        RzPackage rzPackage = new RzPackage();
        BeanUtil.copyProperties(request, rzPackage);

        return saveOrUpdate(rzPackage);
    }

    /**
     * 删除套餐
     *
     * @param id 套餐id
     * @return boolean
     */
    @Override
    public boolean deletePackage(Integer id) {
        RzPackage rzPackage = baseMapper.selectById(id);
        if(ObjectUtil.isNull(rzPackage)){
            throw new CrmebException("该套餐不存在");
        }
        rzPackage.setIsDelete(true);
        rzPackage.setUpdateTime(new Date());
        return updateById(rzPackage);
    }

    /**
     * 套餐分页列表
     *
     * @param request 套餐分页列表查询请求对象
     * @return CommonPage<RzPackageResponse>
     */
    @Override
    public CommonPage<RzPackageResponse> getPackageList(SearchRzPackageRequest request) {
        // 创建分页对象
        Page<RzPackage> page = new Page<>(request.getPage(), request.getLimit());

        // 构建查询条件
        LambdaQueryWrapper<RzPackage> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RzPackage::getIsDelete, false)
                .orderByDesc(RzPackage::getCreateTime);

        // 执行分页查询
        IPage<RzPackage> rzPackagePage = this.page(page, wrapper);

        List<RzPackageResponse> rzPackageResponseList = rzPackagePage.getRecords().stream()
                .map(rzPackage -> {
                    RzPackageResponse response = new RzPackageResponse();
                    BeanUtil.copyProperties(rzPackage, response);
                    return response;
                })
                .collect(Collectors.toList());

        // 创建响应分页对象
        IPage<RzPackageResponse> respPage = new Page<>();
        BeanUtils.copyProperties(rzPackagePage, respPage);
        respPage.setRecords(rzPackageResponseList);

        return CommonPage.restPage(respPage);
    }

    /**
     * 套餐名称重复校验
     *
     * @param request 套餐名称
     */
    private void validateName(RzPackageRequest request) {
        LambdaQueryWrapper<RzPackage> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RzPackage::getName, request.getName());
        RzPackage rzPackage = getOne(wrapper);
        if (ObjectUtil.isNotNull(rzPackage)) {
            if(ObjectUtil.isNotNull(request.getId())) {
                if (rzPackage.getId() != request.getId()) {
                    throw new CrmebException("套餐名称重复");
                }
            }else {
                throw new CrmebException("套餐名称重复");
            }
        }
    }
}
